/*
    This file is part of Epoch <http://code.google.com/p/epoch/>.
    
    Copyright (C) 2011  Adam Meily <meily.adam@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include "ShareDirectory.h"
#include <qshareddata.h>
/*
 * path = /asd/dsfg
 *    -> don't begin or end with /
 */
ShareDirectory::ShareDirectory(const ShareDirectory& parent, const QString& dir)
        : m_share(parent.m_share), m_path(parent.getPath(dir))
{
}

ShareDirectory::ShareDirectory(const Share& share, const QString& path)
        : m_share(share), m_path(path)
{
}


ShareDirectory::ShareDirectory(const ShareDirectory& other)
        : m_share(other.m_share), m_path(other.m_path)

{
}

ShareDirectory::~ShareDirectory()
{
}

ShareDirectory& ShareDirectory::operator=(const ShareDirectory& other)
{
    m_share = other.m_share;
    m_path = other.m_path;
    return *this;
}

ShareDirectory ShareDirectory::cd(const QString& dir) const
{
    return ShareDirectory(m_share, getPath(dir));
}

QString ShareDirectory::getPath(const QString& name) const
{
    if(name.length() == 0)
        return m_path;
    
    QString d = name;
    if(d[0] != '/')
        d.prepend('/');
    
    if(d[d.length()-1] == '/')
        d.remove(d.length() - 1, 1);
    
    return m_path + d;
}

bool ShareDirectory::isRoot() const
{
    return m_path == "/";
}

ShareDirectory ShareDirectory::parent()
{
    if(isRoot())
        return *this;
    
    int i = m_path.lastIndexOf('/');
    QString path = m_path.left(i);
    return ShareDirectory(m_share, path);
}

const QString& ShareDirectory::path() const
{
    return m_path;
}

QString ShareDirectory::name() const
{
    if(isRoot())
        return QString();
    
    int i = m_path.lastIndexOf('/');
    return m_path.right(m_path.length() - i + 1);
}


/*
QString ShareDirectory::url()
{
    // epoch://share/dir
    return QString("epoch://%1%2").arg(share().name()).arg(m_path);
}
*/

